package com.example.listening.examsdk.core;

import android.text.TextUtils;

import com.example.listening.examsdk.ExamSDK;

import org.json.JSONObject;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tong on 15/11/18.
 * 题目信息
 */
public class Subject implements Serializable {
    private final String uid;
    private String jsonString;
    /**
     * 宿主文件相对路径
     */
    private String location;

    /**
     * 在父节点的id
     */
    private String id;

    //head
    /**
     * 题目类型
     */
    private SubjecType type;

    private Section section;

    private int ansQue;

    private String allPic;

    private String allSound;

    private int allReplay;

    private int allAnsTime;

    //body
    /**
     * 问题描述
     */
    private String sub;

    private String sound;

    private String pic;

    private int replay;

    private int ansTime;

    /**
     * 选择题的选项
     */
    private List<String> options = new ArrayList<String>();

    //key
    /**
     * 正确答案
     */
    private String answer;

    /**
     * 词汇
     */
    private String word;

    /**
     * 解析
     */
    private String content;

    /**
     * 技巧
     */
    private String skill;

    /**
     * 详解
     */
    private String point;

    public Subject(String location, String uid, String subjectId,Section section, JSONObject subjectObj) {
        this.jsonString = subjectObj.toString();
        this.location = location;
        this.id = subjectId;
        this.section = section;
        this.uid = uid;

        JSONObject headObj = subjectObj.optJSONObject("Head");
        this.type = SubjecType.valueOf(headObj.optInt("Type2"));
        this.ansQue = headObj.optInt("AnsQue");
        this.allPic = headObj.optString("AllPic");
        this.allSound = headObj.optString("AllSound");
        this.allAnsTime = headObj.optInt("AllAnsTime");
        this.allReplay = headObj.optInt("AllReplay");

        //==
        JSONObject bodyObj = subjectObj.optJSONObject("Body");
        this.sub = bodyObj.optString("Sub");
        this.pic = bodyObj.optString("Pic");
        this.sound = bodyObj.optString("Sound");
        this.ansTime = bodyObj.optInt("AnsTime");
        this.replay = bodyObj.optInt("Replay");
        for (int i = 0;i < SubjecType.getMaxOptions();i++) {
            String value = bodyObj.optString("An" + (i + 1));
            options.add(value != null ? value : "");
        }

        //==
        JSONObject keyObj = subjectObj.optJSONObject("Key");
        this.answer = keyObj.optString("Ans");
        this.word = keyObj.optString("Word");
        this.content = keyObj.optString("Content");
        this.skill = keyObj.optString("Skill");
        this.point = keyObj.optString("Point");

        ExamSDK.d("Subject init location: " + location + " id: " + id + " obj: " + subjectObj);

//        "Q": {
//            "Head": {
//                "Part": "����ѵ��",
//                        "Type1": "Pic",
//                        "Type2": "3",
//                        "Rem": "PartA-1",
//                        "AnsQue": "1",
//                        "Txt": "",
//                        "AllPic": "Q21_1_1_000011.png",
//                        "AllSound": "",
//                        "AllReplay": "0",
//                        "AllAnsTime": "0"
//            },
//            "Body": {
//                "Sub": "Why did the man go to hospital?",
//                        "Pic": "",
//                        "Sound": "Q21_1_1_000011.mp3",
//                        "Replay": "1",
//                        "AnsTime": "2",
//                        "An1": "",
//                        "An2": "",
//                        "An3": "",
//                        "An4": ""
//            },
//            "Key": {
//                "Ans": "B",
//                        "Word": "",
//                        "Content": "M:Excuse me,could you please tell me how to get to the hospital?I have a serious toothache.\nB:Sure.You can take bus No.40. Look! The bus stop is over there.\nQuestion:Why did the man go to hospital?",
//                        "Skill": "",
//                        "Point": "���鿼�����ϸ�ڵȹؼ���Ϣ�Ĳ�׽������",
//                        "S1": "0",
//                        "S2": "0",
//                        "S3": "1",
//                        "S4": "0",
//                        "S5": "0",
//                        "Frequency": ""
//            }
//        }
    }

    public String getLocation() {
        return location;
    }

    public String getId() {
        return id;
    }

    public String getSub() {
        return sub;
    }

    public List<String> getOptions() {
        return options;
    }

    public String getAnswer() {
        return answer;
    }

    public SubjecType getType() {
        return type;
    }

    public String getWord() {
        return word;
    }

    public String getContent() {
        return content;
    }

    public String getSkill() {
        return skill;
    }

    public String getPoint() {
        return point;
    }


    public File getAllPicFile() {
        if (TextUtils.isEmpty(allPic)) {
            return null;
        }
        File file = new File(ExamSDK.getPathStrategy().getRootPath(),getLocation() + "/" + this.allPic);
        return file;
    }

    public File getAllSoundFile() {
        if (TextUtils.isEmpty(allSound)) {
            return null;
        }
        File file = new File(ExamSDK.getPathStrategy().getRootPath(),getLocation() + "/" + this.allSound);
        return file;
    }

    public File getSoundFile() {
        if (TextUtils.isEmpty(sound)) {
            return null;
        }
        File file = new File(ExamSDK.getPathStrategy().getRootPath(),getLocation() + "/" + this.sound);
        return file;
    }

    public File getPicFile() {
        if (TextUtils.isEmpty(pic)) {
            return null;
        }
        File file = new File(ExamSDK.getPathStrategy().getRootPath(),getLocation() + "/" + this.pic);
        return file;
    }

    public String getAllPic() {
        return allPic;
    }

    public String getAllSound() {
        return allSound;
    }

    public int getAllReplay() {
        return allReplay;
    }

    public int getAllAnsTime() {
        return allAnsTime;
    }

    public String getSound() {
        return sound;
    }

    public String getPic() {
        return pic;
    }

    public int getReplay() {
        return replay;
    }

    public int getAnsTime() {
        return ansTime;
    }

    public Section getSection() {
        return section;
    }

    public String getJsonString() {
        return jsonString;
    }

    public String getUid() {
        return uid;
    }

    public List<MediaItem> getAllMediaItem() {
        List<MediaItem> fileList = new ArrayList<MediaItem>();
        if (this.ansQue == 1) {
            File file = this.getAllSoundFile();
            if (file != null) {
                fileList.add(new MediaItem(file,allReplay,allAnsTime));
            }
        }
        File file = this.getSoundFile();
        if (file != null) {
            fileList.add(new MediaItem(this.getSoundFile(),replay,ansTime));
        }
        return fileList;
    }

    public List<MediaItem> getAllMediaItem2() {
        List<MediaItem> fileList = new ArrayList<MediaItem>();
        File file = this.getAllSoundFile();
        if (file != null) {
            fileList.add(new MediaItem(file));
        }

        file = this.getSoundFile();
        if (file != null) {
            fileList.add(new MediaItem(this.getSoundFile()));
        }
        return fileList;
    }

    @Override
    public String toString() {
        return "Subject{" +
                "location='" + location + '\'' +
                ", id='" + id + '\'' +
                ", type=" + type +
                ", section=" + section +
                ", ansQue=" + ansQue +
                ", allPic='" + allPic + '\'' +
                ", allSound='" + allSound + '\'' +
                ", allReplay=" + allReplay +
                ", allAnsTime=" + allAnsTime +
                ", sub='" + sub + '\'' +
                ", sound='" + sound + '\'' +
                ", pic='" + pic + '\'' +
                ", replay=" + replay +
                ", ansTime=" + ansTime +
                ", options=" + options +
                ", answer='" + answer + '\'' +
                ", word='" + word + '\'' +
                ", content='" + content + '\'' +
                ", skill='" + skill + '\'' +
                ", point='" + point + '\'' +
                '}';
    }
}
